<?php
/**
 * @ClassName: Index
 * @Description: TODO
 * @author: wyaohu
 * @Date: 2024/9/8 21:08
 * @Version: V1.0.0
 */

namespace app\api\controller;

use app\common\model;
use think\Controller;

class Index extends Controller
{
    public function index()
    {
        // 首页轮播图
        $swiper = $this->getCmsBanner(1, "swiper");

        // 首页菜单
        $menu = $this->getCmsBanner(3, "menu");

        // 快讯
        $systemNotice = $this->systemNotice();

        // 专区商品（智能水架）列表
        $zq_products = $this->getGoodsList(1, "zq_products");
        $zhuanqu = [
            'name' => '智能水架',
            'tags' => '水架专区',
            'link' => '/lifePages/water-rack/water-rack',
            'desc' => '优选水架 注重品质',
            'list' => $zq_products,
        ];

        // 推荐商品列表
        $tj_products = [];//$this->getGoodsList(2, "tj_products");

        // 热门商品列表
        $hot_products = [];//$this->getGoodsList(3, "hot_products");

        // 创业礼包列表
        $gift_products = $this->getGoodsList(4, "gift_products");
        $giftpack = [
            'name' => '升级礼包',
            'tags' => '礼包专区',
            'link' => '/lifePages/gift-pack/gift-pack',
            'desc' => '',
            'list' => $gift_products,
        ];

        // 积分商品列表
        $points_products = $this->getGoodsList(5, "points_products");

        // 返回数据
        return json(
            [
                'code' => 1,
                'msg' => 'ok',
                'data' => [
                    'swiper' => $swiper,
                    'menu' => $menu,
                    'systemNotice' => $systemNotice,
                    'zhuanqu' => $zhuanqu,
                    'tj_products' => $tj_products,
                    'hot_products' => $hot_products,
                    'giftpack' => $giftpack,
                    'points_products' => $points_products,
                ]
            ]
        );
    }

    /**
     * 获取主题
     * @return \think\response\Json
     */
    public function getTheme()
    {
        $data = model\Theme::where(['status' => 1])->find();
        if ($data) {
            $data['bg_pic'] = replace_url($data['bg_pic']);
        }

        return json([
            'code' => 1,
            'data' => $data,
        ]);
    }

    /**
     * 获取系统信息
     * @return \think\response\Json
     */
    public function getSystemInfo()
    {
        $data = web_config('web_config');

        return json([
            'code' => 1,
            'data' => $data,
        ]);
    }

    /**
     * 获取引导图
     * @return \think\response\Json
     */
    public function appGuide()
    {
        $swiper = model\CmsBanner::where(['position_id' => 2, 'is_show' => 1])
//            ->cache('appGuide', mt_rand(7000, 8000))
            ->order('sort')
            ->field('id,title,link,image')
            ->select();

        foreach ($swiper as $key => $value) {
            $swiper[$key]['image'] = replace_url($value['image']);
        }

        return json([
            'code' => 1,
            'data' => $swiper,
        ]);
    }

    /**
     * 获取APP版本信息
     * @return \think\response\Json
     */
    public function getAppVersion()
    {
        $version = input('version', '1.0.0');
        $appType = input('appType', 'user');
        $platformType = input('platformType', 'android');

        $current = intval(str_replace(".", "", $version));

        $info = model\AppVersion::where("platform_type", $platformType)->where('app_type', $appType)->find();

        if ($info) {
            if ($current < $info['version_code']) {
                $info['isUpdate'] = true;
                return json([
                    'code' => 1,
                    "data" => $info,
                ]);
            } else {
                $info['isUpdate'] = false;
                return json([
                    'code' => 1,
                    "data" => $info,
                ]);
            }
        } else {
            return json([
                'code' => 0,
                "data" => '没有版本号信息',
            ]);
        }
    }

    protected function getCmsBanner($position_id = 1, $cache_key = "cache_key", $isList = 1)
    {
        $where = [];
        $where[] = ["position_id", '=', $position_id];
        $where[] = ["is_show", '=', 1];

        $field = "id,title,link,image,description,flag";

        if ($isList) {
            $advList = model\CmsBanner::where($where)
                ->cache($cache_key, 60)
                ->order('sort')
                ->field($field)
                ->select();
        } else {
            $advList = model\CmsBanner::where($where)
                ->cache($cache_key, 60)
                ->order('sort')
                ->field($field)
                ->find();
        }

        return $advList;
    }

    /**
     * 快讯
     * @return model\Notice[]|array|\think\Collection
     */
    protected function systemNotice()
    {
        $list = model\Notice::where("is_show", 1)
            ->where('type', 0)
            ->order("create_time desc")
            ->limit(0, 5)
            ->select();

        if (count($list) == 0) {
            $list = [
                'id' => 0,
                'title' => "暂无快讯"
            ];
        }

        return $list;
    }

    protected function getGoodsList($tags = 0, $cache_key = "cache_key", $need_show = 1)
    {
        $where = [];
        $where[] = ["on_sale", "=", 1];

        // 商品是否需要设置显示，才获取:默认显示才获取
        if ($need_show) {
            $where[] = ["is_show", "=", 1];
        } else {
            $where[] = ["is_show", "=", 0];
        }

        switch ($tags) {
            case 1:
                // 专区商品（智能水架）
                $where[] = ["goods_type", "=", 5];
                $where[] = ['category_id', '=', 5];
                break;
            case 2:
                // 推荐商品
                $where[] = ["goods_type", "=", 0];
                $where[] = ['is_recommend', '=', 1];
                break;
            case 3:
                // 热门商品
                $where[] = ["goods_type", "=", 0];
                $where[] = ['is_hot', '=', 1];
                break;
            case 4:
                // 创业礼包
                $where[] = ["goods_type", "=", 7];
                $where[] = ['category_id', '=', 6];
                break;
            case 5:
                // 积分兑换商品
                $where[] = ["goods_type", "=", 1];//商品类型，0普通商品，1积分商品，2酒店，4会员票，5水架设备，6水产品，7虚拟商品
                $where[] = ["exchange_type", "<>", 0];//兑换类型，0无，1积分，2积分+现金
                $where[] = ['points', '>', 0];//兑换积分
                break;
            default:
                break;
        }

        $order = "is_top desc,is_recommend desc,is_hot desc,sort";
//        var_dump(model\ShopGoods::where($where)->cache($cache_key, 60)->order($order)->limit(0, 20)->fetchSql()->select());
        $goods_list = model\ShopGoods::where($where)->cache($cache_key, 60)->order($order)->limit(0, 20)->select();

        return $goods_list;
    }

}